<?php include('config.php');

include('./class/class.database.php');

    if (isset($debug_))
    {
    ini_set ("display_errors", "1");
    error_reporting(E_ALL);
	}

  $time = microtime();
  $time = explode(' ', $time);
  $time = $time[1] + $time[0];
  $start = $time;
  
  $db = new database($server, $username, $password, $database);
  $db->connect(database);
  //Cookie login?
  if (isset($_COOKIE['bb_auth']) and $_SESSION['signed_in'] == false) {
      $cookie_ = mysql_real_escape_string($_COOKIE['bb_auth']);
      $result = $db->query("SELECT * FROM " . $table_prefix . "users WHERE session = '$cookie_' LIMIT 1");
      if (mysql_num_rows($result) >= 1) {
          $row = $db->fetch_array($result, 'assoc');
          $_SESSION['signed_in'] = true;
          $_SESSION['user_id'] = mysql_real_escape_string($row['user_id']);
          $_SESSION['user_name'] = mysql_real_escape_string($row['user_name']);
          $_SESSION['user_level'] = mysql_real_escape_string($row['user_level']);
          $_SESSION['user_ip'] = $_SERVER["REMOTE_ADDR"];
          $_SESSION['user_lang'] = mysql_real_escape_string($row['user_lang']);
          $result2 = $db->query("UPDATE " . $table_prefix . "users SET user_last_login = NOW() + INTERVAL $server_time MINUTE + INTERVAL $time_difference HOUR WHERE user_id = " . $_SESSION['user_id'] . "");
          //$sql2 = $db->query("DELETE FROM " . $table_prefix . "online_users WHERE user_id = '" . $_SESSION['user_id'] . "'") or die(mysql_error());
          //Delete user activity on login
          $delete_activities = $db->query("DELETE FROM " . $table_prefix . "online_users WHERE user_id = '" . $_SESSION['user_id'] . "'") or die(mysql_error());
      }
  }
  
if (isset($_SESSION['user_lang']) AND $_SESSION['user_lang']!="")
  {include('lang/' . $_SESSION['user_lang'] . '.php');}
  
  if (!isset($_SESSION['user_lang']) or $_SESSION['user_lang']=="")
  {include('lang/' . $def_language . '.php');}
  //END cookie login
  if (!isset($_SESSION['signed_in'])) {
      $_SESSION['user_level'] = null;
  }
  if (isset($_GET['print'])) {
      $style = "print.css";
  } else {
      $style = "style.css";
  }
  if (isset($_GET['page'])) {
      $page = mysql_real_escape_string($_GET['page']);
  } else {
      $page = "";
  }
  
  //COMMON VARS
  $hide_footer = 0;
  $qqq = 0;
  $current_page = 1;
  $syntax_highlight = "";
  $headerlogo = '<img align="absmiddle" src="style/'.$default_style.'/img/brevisbb.png">';
  $shadow_text = 'text-shadow: 3px 2px 0 #000;';
  $f_color = "#fff";
  $registration = "<a class='menuButtons' href='signup.php'>$l_register</a>";
  $statistics = "<a class='menuButtons' href='stats.php'>$l_stats</a>";
  $topicid = "";
  $fid = "";
  $pageTitle ="";
  $search_in ="";
  $condition = "";
  $condition2 = "";
  $u_id = "";
  $pn = "";

  if (isset($_GET['f'])) {$fid = mysql_real_escape_string($_GET['f']);}
  if (isset($_GET['t'])) {$topicid = mysql_real_escape_string($_GET['t']);}
  if (isset($_GET['page'])) {$page = mysql_real_escape_string($_GET['page']);}
  if (isset($_GET['u'])) {$u_id = mysql_real_escape_string($_GET['u']);}
  if (isset($_GET['pn'])) {$pn = mysql_real_escape_string($_GET['pn']);}
  //END COMMON VARS
  
  //CHECK BANS
  if (!isset($_SESSION['ok'])) {
      $result = $db->query("SELECT * FROM " . $table_prefix . "banlist WHERE ban_ip = '" . $_SERVER["REMOTE_ADDR"] . "'");
      
      if (mysql_num_rows($result) == 0) {
          $_SESSION['ok'] = $_SERVER["REMOTE_ADDR"];
      } else {
          $bans = $db->fetch_array($result, 'assoc');
          $ban_time = date($date_format, strtotime($bans['ban_time']));
          $ban_str = "(Expires: " . $ban_time . ")";
          $time = strtotime($time_difference . " hours");
          if ($bans['ban_time'] == "0000-00-00 00:00:00") {
              $ban_str = "";
          }
          
          $time = strtotime($time_difference . " hours");
          if (date($date_format, $time) >= $ban_time and $bans['ban_time'] != "0000-00-00 00:00:00") {
              $result2 = mysql_query("DELETE FROM " . $table_prefix . "banlist WHERE ban_ip = '" . $_SERVER["REMOTE_ADDR"] . "'");
              header('location: index.php');
          }
          $pageTitle = "$l_you_banned";
          $pageContents = ob_get_contents();
          ob_end_clean();
          echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
          echo "<div align='center'><br /><b>$l_you_banned</b><br /><br />" . $ban_str . "</div>";
          if ($bans['ban_reason'] != "") {
              echo "<br /><div align='center'><b>$l_ban_reason</b> " . $bans['ban_reason'] . "</div><br /><br />";
          }
          $_SESSION['signed_in'] = null;
          die;
      }
  }
  //END CHECK BANS
  
  //Quick parse template
  $login_ = "<a class='menuButtons' href='signin.php?t=(T_ID)&page={P_ID}'>$l_login</a>";
  if (isset($_GET['t'])) {
      $topicid = mysql_real_escape_string($_GET['t']);
      $syntax_highlight = '
    <link rel="stylesheet" href="./style/{STYLE}/highlight.css" type="text/css">
    <script type="text/javascript" src="style/shCore.js"></script>
  <script type="text/javascript" src="style/shBrushPhp.js"></script>
  <script type="text/javascript">  SyntaxHighlighter.config.stripBrs = true;

  SyntaxHighlighter.all();</script>';
  }
  
  $tags = array('{F_COLOR}', '{S_TEXT}', '{LOGO}', '{SYNTAX_HIGHLIGHT}', '{LOGIN_STYLE}', '{STYLE}', '{START}', '{F_NAME}', '{F_HOME}', '{CREATE_T}', '(CREATE_T_L)', '{MEMBERS}', '{IF_LOGIN}', '{END_IF}', '{IF_NOT_LOGIN}', '{END_NOT}', '{P_ID}', '{SEARCH}', '{PROFILE}', '{STYLE_CSS}', '{METAKEY}', '{METADESC}', '{IMGSIZE}', '{CLICK_TITLE}', '{STATS}');
  
  if (isset($_GET['print'])) {
      $l_login = "";
      $l_home = "";
      $l_or = "";
      $registration = "";
      $l_new_topic = "";
      $l_search = "";
      $l_members = "";
      $l_login = "";
      $l_profile = "";
      $headerlogo = "";
      $shadow_text = "";
      $f_color = "#000";
  }
  
  $data = array($f_color, $shadow_text, $headerlogo, $syntax_highlight, $login_, $default_style, ob_start(), $l_forumname, $l_home, $l_new_topic, 'create_topic.php?f=' . $fid, $l_members, '<!--', '-->', '', '', $page, $l_search, '', $style, $l_keywords, $l_forumdesc, $max_imagesize_in_posts, $l_click_image, $statistics);
  
  if (isset($_SESSION['signed_in'])) {
      $tags = array('{F_COLOR}', '{S_TEXT}', '{LOGO}', '{SYNTAX_HIGHLIGHT}', '{LOGIN_STYLE}', '{STYLE}', '{START}', '{F_NAME}', '{F_HOME}', '{CREATE_T}', '(CREATE_T_L)', '{MEMBERS}', '{IF_LOGIN}', '{END_IF}', '{HELLO}', '{USER_SID}', '{L_NOTU}', '{SIGNOUT}', '{IF_NOT_LOGIN}', '{END_NOT}', '{P_ID}', '{SEARCH}', '{PROFILE}', '{STYLE_CSS}', '{METAKEY}', '{METADESC}', '{IMGSIZE}', '{CLICK_TITLE}', '{STATS}');
      
      if (isset($_GET['print'])) {
          $l_login = "";
          $l_home = "";
          $l_or = "";
          $registration = "";
          $l_new_topic = "";
          $l_search = "";
          $l_members = "";
          $l_login = "";
          $l_profile = "";
          $headerlogo = "";
          $shadow_text = "";
          $f_color = "#000";
      }
      
      $data = array($f_color, $shadow_text, $headerlogo, $syntax_highlight, $login_, $default_style, ob_start(), $l_forumname, $l_home, $l_new_topic, 'create_topic.php?f=' . $fid, $l_members, '', '', $l_hello, htmlentities($_SESSION['user_name']), $l_notu, $l_signout, '<!--', '-->', $page, $l_search, '<a class="menuButtons" href="profile.php?profile=1">' . $l_profile . '</a>', $style, $l_keywords, $l_forumdesc, $max_imagesize_in_posts, $l_click_image, $statistics);
  } else {
      $tags = array('{F_COLOR}', '{S_TEXT}', '{LOGO}', '{SYNTAX_HIGHLIGHT}', '{LOGIN_STYLE}', '{STYLE}', '{START}', '{F_NAME}', '{F_HOME}', '{CREATE_T}', '(CREATE_T_L)', '{MEMBERS}', '{IF_LOGIN}', '{END_IF}', '{LOGIN}', '{OR}', '{REGISTER}', '{F_ID}', '(T_ID)', '{IF_NOT_LOGIN}', '{END_NOT}', '{P_ID}', '{SEARCH}', '{PROFILE}', '{STYLE_CSS}', '{METAKEY}', '{METADESC}', '{IMGSIZE}', '{CLICK_TITLE}', '{STATS}');
      
      if (isset($_GET['print'])) {
          $l_login = "";
          $l_home = "";
          $l_or = "";
          $registration = "";
          $l_new_topic = "";
          $l_search = "";
          $l_members = "";
          $l_login = "";
          $l_profile = "";
          $headerlogo = "";
          $shadow_text = "";
          $f_color = "#000";
      }
      if ($allow_registration != 1) {
          $registration = "";
          $l_or = "";
      }
      $data = array($f_color, $shadow_text, $headerlogo, $syntax_highlight, $login_, $default_style, ob_start(), $l_forumname, $l_home, $l_new_topic, 'create_topic.php?f=' . $fid, $l_members, '<!--', '-->', $l_login, $l_or, $registration, $fid, $topicid, '', '', $page, $l_search, '', $style, $l_keywords, $l_forumdesc, $max_imagesize_in_posts, $l_click_image, $statistics);
  }
  echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/header.html"));
?>
      <div id="content">
<?php
  if (!isset($_GET['print'])) {
      echo "<table><tr>";
      echo " <td class='headerBar' align='left'><a class='underline' href='view_new.php?sort=desc'>$l_view_new_posts</a>";
      echo " | <a class='underline' href='view_today.php?sort=desc'>$l_view_today_posts</a>
 | <a class='underline' href='view_unanswered.php'>$l_view_unnanswered</a>";
      $mcp = "";
      if ($_SESSION['user_level'] >= 1) {
          $mcp = " | <a class='underline' href='mcp.php?reports=1'>$l_moderatorcp</a>";
      }
      
      if (isset($_SESSION['signed_in'])) {
          echo "<td class='headerBar' align='right'> <a class='underline' href='view_posts.php?u=" . $_SESSION['user_id'] . "'>$l_view_my</a> $mcp </td>";
      }
      //if ($_SESSION['signed_in'] == true and $_SESSION['user_level'] == 1) {
      // echo " <a class='underline' href='create_cat.php'> | " . $l_create_category . "</a></td>";
      // }
      echo "</tr></table>";
  }
  if (file_exists('./install/index.php') or file_exists('./install')) {
      echo "<br><br><div align='center'>Please remove <b>install</b> directory</div><br><br>";
      die;
  }
  
  $timezone = new DateTimeZone("Europe/Berlin");
  $date = new DateTime();
  $date->setTimezone($timezone);
  echo "<div align='right'> $l_today_is " . $date->format($date_format) . "</div>";
  $yesterday = $date->format(mktime(date("H"), date("i"), date("s"), date("m"), date("d") - 1, date("Y"), -1));
  
  //echo date($date_format,$yesterday);
  
  //echo "Debug: time difference ".ceil($s_timediff). " minutes";
  
  //echo "<table><tr><td class='headerBar' height='16px'></td></tr></table>";
?>